﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Spatial;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeManagement
{
    public partial class EditWardForm : Form,IRecordPoint
    {
        public ActionType FormType
        {
            get;
            set;
        }
        public Form EditWardFormParent
        {
            get;
            set;
        }
        public EditWardForm()
        {
            InitializeComponent();
        }

        private void EditWardForm_Load(object sender, EventArgs e)
        {

            if (ActionType.NEW_WARD == this.FormType)
            {
                button1.Text = "Thêm";
                txtID.Enabled = false;
                txtPoint.Text = "(";
            }
            using (var context = new TreeDBEntities())
            {
                List<District> districts = context.Districts.ToList();
                viewPanel1.Wards = context.Wards.ToList();
                cbbDistrict.DataSource = districts;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ActionType.NEW_WARD == this.FormType)
            {
                String strPolygon = "POLYGON " + "(" + txtPoint.Text + ")";                
                DbGeometry geometry = DbGeometry.FromText(strPolygon, 0);
                using (var context = new TreeDBEntities())
                {
                    context.Wards.Add(new Ward{
                        Name = txtName.Text,
                        District = (cbbDistrict.SelectedItem as District).ID,
                        Polygon = geometry
                    });
                    context.SaveChanges();
                }
                (this.EditWardFormParent as WardForm).reload();
                this.Close();
            }
        }

        

        private void btnLoadFromMap_Click(object sender, EventArgs e)
        {
            if ((sender as Button).Tag == null)
            {
                viewPanel1.registerRecodePoint(this);
                (sender as Button).Tag = 1;
                (sender as Button).Text = "Xóa";
            }
            else
            {
                txtPoint.Text = "(";
            }
            viewPanel1.RecordedPoints.Clear();
            viewPanel1.Refresh();
        }

        public void notifyRecordPoint(Point point)
        {
            point = TMUtils.getValidPoints(point, viewPanel1.RecordedPoints);
            viewPanel1.RecordedPoints.Add(point);
            txtPoint.Text = TMUtils.addStringFromRecordedPoint(txtPoint.Text, point);
            
            viewPanel1.Refresh();
        }


        public void drawRecordedPoint(Graphics g)
        {
            Brush pen = new SolidBrush(Color.Black);
            foreach (Point p in viewPanel1.RecordedPoints)
            {
                g.FillEllipse(pen, p.X - TMConsts.POINT_SIZE / 2, p.Y - TMConsts.POINT_SIZE / 2, TMConsts.POINT_SIZE, TMConsts.POINT_SIZE);
            }
            //draw line
            TMUtils.drawLine(g, viewPanel1.RecordedPoints);
            //fill polygon if there are a cycle
            if(TMUtils.checkPointsHaveCycle(viewPanel1.RecordedPoints))
                TMUtils.drawPolygon(g, viewPanel1.RecordedPoints);
        }
    }
}
